﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SES.CMS.DO;
using SES.CMS.BL;

namespace SES.CMS.Module
{
    public partial class ucThuongHieu : System.Web.UI.UserControl
    {
        private static string url = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["LoaiSP"] != null)
                {
                    loadTH2();
                    Session["DuongDan"] = Server.HtmlEncode(Request.RawUrl);
                    Session["LoaiSP"] = Request.QueryString["LoaiSP"].ToString();
                }
                //else if (Request.QueryString["Filter"] != null)
                //{
                //    Session["DuongDan"] = Server.HtmlEncode(Request.RawUrl);
                //    rpt.Visible = false;
                //    rpt2.Visible = false;
                //    imgbt.Visible = true;
                //    lbNH.Visible = true;
                //    cmsNhanHieuDO obj = new cmsNhanHieuBL().Select(new cmsNhanHieuDO { NhanHieuID = int.Parse(Session["NhanHieu"].ToString()) });
                //    lbNH.Text = obj.TenNhanHieu;
                //}
                else
                {
                    if (Request.QueryString["NhanHieu"] == null)
                    {
                        if (Request.QueryString["Filter"] != null)
                        {
                            loadTH2();
                            Session["DuongDan"] = Server.HtmlEncode(Request.RawUrl);
                        }
                        else if (Request.QueryString["FilterDM"] != null)
                        {
                            loadTH();
                            //rpt.Visible = false;
                            //rpt2.Visible = false;
                            //imgbt.Visible = true;
                            //lbNH.Visible = true;
                            //cmsNhanHieuDO obj = new cmsNhanHieuBL().Select(new cmsNhanHieuDO { NhanHieuID = int.Parse(Session["NhanHieu"].ToString()) });
                            //lbNH.Text = obj.TenNhanHieu;
                        }
                        else if((Request.QueryString["FilterDM"] == null)&&(Request.QueryString["Filter"] == null))
                        {
                            loadTH();
                            Session["DuongDan"] = Server.HtmlEncode(Request.RawUrl);
                        }
                    }

                    else
                    {
                        loadTH();
                        //rpt.Visible = false;
                        //rpt2.Visible = false;
                        //imgbt.Visible = true;
                        //lbNH.Visible = true;
                        //cmsNhanHieuDO obj = new cmsNhanHieuBL().Select(new cmsNhanHieuDO { NhanHieuID = int.Parse(Request.QueryString["NhanHieu"].ToString()) });
                        //lbNH.Text = obj.TenNhanHieu;
                    }
                }
            }
        }

        public void loadTH()
        {
            rpt2.Visible = false;
            rpt.Visible = true;
            imgbt.Visible = false;
            lbNH.Visible = false;
            DataTable dt = new cmsNhanHieuBL().SelectAll();
            rpt.DataSource = dt;
            rpt.DataBind();
            if ((Request.QueryString["FilterDM"] != null) || (Request.QueryString["NhanHieu"] != null))
            {
                int id = 0;
                if (Request.QueryString["FilterDM"] != null)
                {
                    if (Session["NhanHieu"] != null)
                        id = int.Parse(Session["NhanHieu"].ToString());
                }
                else
                    id = int.Parse(Request.QueryString["NhanHieu"].ToString());
                int i = 0;
                foreach (DataRow dr in dt.Rows)
                {
                    if (int.Parse(dr.ItemArray[0].ToString()) == id)
                        //((ImageButton)e.Item.FindControl("imgbt2")).Attributes.Add("Visible", "true");
                        ((ImageButton)rpt.Items[i].FindControl("imgbt2")).Visible = true;
                    i++;
                }
            }
        }

        public void loadTH2()
        {
            rpt2.Visible = true;
            rpt.Visible = false;
            imgbt.Visible = false;
            lbNH.Visible = false;
            rpt2.DataSource = new cmsNhanHieuBL().SelectAll();
            rpt2.DataBind();
        }
        protected string FriendlyUrl(string strTitle)
        {
            return Ultility.Change_AV(strTitle);
        }

        protected void imgbt_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("/SanPham.aspx");
        }
    }
}